//
//  ViewController.m
//  kabu
//
//  Created by XiangDongMBP on 2016/12/20.
//  Copyright © 2016年 East. All rights reserved.
//

#import "ViewController.h"
#import <AFNetworking.h>
#import "RSA.h"
#import <UIImageView+AFNetworking.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loginBtnTapped:(id)sender {

    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSInteger timeInt = time;
    NSDictionary *tokenDic = @{@"password" : @"123456",
                               @"timestamp": @(timeInt)};
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:tokenDic options:NSJSONWritingPrettyPrinted error:nil];

    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"pem"];
    NSString *pubKey = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:nil];
    NSData * encryptData = [RSA encryptData:jsonData publicKey:pubKey];
    NSString * base64JsonStr = [encryptData base64EncodedStringWithOptions:0];
    NSLog(@"加密后的字符串是(%@)",base64JsonStr);
    
    NSString * passwordUrlStr = @"https://app.tokabu.com/api/login";
    NSDictionary *paraDic = @{@"account" : @"admin",
                              @"token" : base64JsonStr};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:passwordUrlStr parameters:paraDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"登陆请求成功,返回数据:(%@, %@)", task, responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"登陆请求失败,(%@)", error);
    }];
}

#pragma mark - life cycle
#pragma mark - delegate
#pragma mark - event response
#pragma mark - private methods
#pragma mark - network request
#pragma mark - getters and setters

@end
